#!/usr/bin/env python3.0

def factor_generator():
    yield 2
    n = 3
    while True:
        yield n
        n += 2

def prime_generator(min=None, max=None):
    lst = [2, 3]
 
    for n in lst:
    
        if max and n > max:
            break
        elif not min or n >= min:
            yield n
    
    else:
        n = n + 2
    
    while not max or n <= max:
    
        if max and n > max:
            break
        else:
            for prime in lst:
                if prime * prime > n:
                    lst.append(n)
                    if not min or n >= min:
                        yield n
                    break
                elif n % prime == 0:
                    break
            n = n + 2

def factorize(n):    
    factors = []
    prime = factor_generator()
    while n > 1:
        p = next(prime)
        e = 0
        while True:
            (q, r) = divmod(n, p)
            if r != 0:
                break
            else:
                n = q
                e += 1
        if e > 0:
            factors.append((p, e))
    
    return factors

def factorial(n):
    fac = 1
    while n > 1:
        fac *= n
        n -= 1
    
    return fac

def bincoeff(n, k):
    result = n - k + 1
    for i in range (2, k+1):
        result *= (n - k + i)
        result //= i
    return result


if __name__ == '__main__':
    print('Result {0}'.format(factorize(315)))
    